import Vue from 'vue'
import App from './App'
import router from './router'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import store from "./store";//这里要导入store
import Vuex from 'vuex' //引入状态管理
import {Loading,Message} from "element-ui";
import JsonViewer from 'vue-json-viewer'

import echarts from "echarts";
import scroll from 'vue-seamless-scroll'


Vue.use(scroll)


Vue.prototype.$echarts = echarts;

// Import JsonViewer as a Vue.js plugin
Vue.use(JsonViewer)

let loadingInstance = null
var axios = require('axios')// 设置反向代理，前端请求默认发送到 http://localhost:8443/api
axios.defaults.baseURL = 'http://localhost:8090'
Vue.prototype.$axios = axios// 全局注册，之后可在其他组件中通过 this.$axios 发送数据

Vue.config.productionTip = false
Vue.use(ElementUI)
Vue.use(Vuex)
// 添加请求拦截器，在请求头中加token
axios.interceptors.request.use(
  config => {
    loadingInstance = Loading.service({
      spinner:'fa fa-spinner fa-spin fa-3x fa-fw',
      text:'拼命加载中。。。'
    })
    if (localStorage.getItem('Authorization')) {
      config.headers.Authorization = localStorage.getItem('Authorization');
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  });
axios.interceptors.response.use(response =>{
  loadingInstance.close()
  if (response.data.code === 200) { // 响应结果里的status: 200是我与后台的约定，大家可以根据实际情况去做对应的判断
    return Promise.resolve(response.data)
  }else if(response.data.code === 401){
    localStorage.removeItem('Authorization')
    Message({
      message :"请先登录",
      type:"error"
    })
    router.replace("/login")
  }else {
    console.log(response.data.code)
    Message({
      message: response.data.message,
      type:"error"
    })
    return Promise.reject(response.data.message)
  }
},error =>{
    Message({
      message :"系统繁忙",
      type:"error"
    })
  return Promise.reject(error)
})



/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store:store,
  render: function (h) { return h(App) },
  components: { App },
  template: '<App/>'
})
